<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='ASC'>/**
</span> * @fileOverview 可排序扩展类
 * @ignore
 */


  var ASC = &#39;ASC&#39;,
    DESC = &#39;DESC&#39;;
<span id='BUI-Data-Sortable'>  /**
</span>   * 排序扩展方法，无法直接使用
   * 请在继承了 {@link BUI.Base}的类上使用
   * @class BUI.Data.Sortable
   * @extends BUI.Base
   */
  var sortable = function(){

  };

  sortable.ATTRS = 

  {
<span id='BUI-Data-Sortable-cfg-compareFunction'>    /**
</span>     * 比较函数
     * @cfg {Function} compareFunction
     * 函数原型 function(v1,v2)，比较2个字段是否相等
     * 如果是字符串则按照本地比较算法，否则使用 &gt; ,== 验证
     */
    compareFunction:{
      value : function(v1,v2){
        if(v1 === undefined){
          v1 = &#39;&#39;;
        }
        if(v2 === undefined){
          v2 = &#39;&#39;;
        }
        if(BUI.isString(v1)){
          return v1.localeCompare(v2);
        }

        if(v1 &gt; v2){
          return 1;
        }else if(v1 === v2){
          return 0;
        }else{
          return  -1;
        }
      }
    },
<span id='BUI-Data-Sortable-cfg-sortField'>    /**
</span>     * 排序字段
     * @cfg {String} sortField
     */
<span id='BUI-Data-Sortable-property-sortField'>    /**
</span>     * 排序字段
     * @type {String}
     */
    sortField : {

    },
<span id='BUI-Data-Sortable-cfg-sortDirection'>    /**
</span>     * 排序方向,&#39;ASC&#39;、&#39;DESC&#39;
     * @cfg {String} [sortDirection = &#39;ASC&#39;]
     */
<span id='BUI-Data-Sortable-property-sortDirection'>    /**
</span>     * 排序方向,&#39;ASC&#39;、&#39;DESC&#39;
     * @type {String}
     */
    sortDirection : {
      value : &#39;ASC&#39;
    },
<span id='BUI-Data-Sortable-cfg-sortInfo'>    /**
</span>     * 排序信息
     * &lt;ol&gt;
     * &lt;li&gt;field: 排序字段&lt;/li&gt;
     * &lt;li&gt;direction: 排序方向,ASC(默认),DESC&lt;/li&gt;
     * &lt;/ol&gt;
     * @cfg {Object} sortInfo
     */
<span id='BUI-Data-Sortable-property-sortInfo'>    /**
</span>     * 排序信息
     * &lt;ol&gt;
     * &lt;li&gt;field: 排序字段&lt;/li&gt;
     * &lt;li&gt;direction: 排序方向,ASC(默认),DESC&lt;/li&gt;
     * &lt;/ol&gt;
     * @type {Object}
     */
    sortInfo: {
      getter : function(){
        var _self = this,
          field = _self.get(&#39;sortField&#39;);

        return {
          field : field,
          direction : _self.get(&#39;sortDirection&#39;)
        };
      },
      setter: function(v){
        var _self = this;

        _self.set(&#39;sortField&#39;,v.field);
        _self.set(&#39;sortDirection&#39;,v.direction);
      }
    }
  };

  BUI.augment(sortable,
  {
    compare : function(obj1,obj2,field,direction){

      var _self = this,
        dir;
      field = field || _self.get(&#39;sortField&#39;);
      direction = direction || _self.get(&#39;sortDirection&#39;);
      //如果未指定排序字段，或方向，则按照默认顺序
      if(!field || !direction){
        return 1;
      }
      dir = direction === ASC ? 1 : -1;

      return _self.get(&#39;compareFunction&#39;)(obj1[field],obj2[field]) * dir;
    },
<span id='BUI-Data-Sortable-method-getSortData'>    /**
</span>     * 获取排序的集合
     * @protected
     * @return {Array} 排序集合
     */
    getSortData : function(){

    },
<span id='BUI-Data-Sortable-method-sortData'>    /**
</span>     * 排序数据
     * @param  {String|Array} field   排序字段或者数组
     * @param  {String} direction 排序方向
     * @param {Array} records 排序
     * @return {Array}    
     */
    sortData : function(field,direction,records){
      var _self = this,
        records = records || _self.getSortData();

      if(BUI.isArray(field)){
        records = field;
        field = null;
      }

      field = field || _self.get(&#39;sortField&#39;);
      direction = direction || _self.get(&#39;sortDirection&#39;);

      _self.set(&#39;sortField&#39;,field);
      _self.set(&#39;sortDirection&#39;,direction);

      if(!field || !direction){
        return records;
      }

      records.sort(function(obj1,obj2){
        return _self.compare(obj1,obj2,field,direction);
      });
      return records;
    }
  });

module.exports = sortable;
</pre>
</body>
</html>
